package com.example.spider.provider.why10w.why10;

import cn.hutool.core.util.ReUtil;
import com.example.spider.provider.why10w.AbstractWhy10wDetailResultHandler;
import com.example.spider.provider.why10w.dto.Why10wDetailTask;
import com.example.spider.provider.why10w.dto.WhyImageInfo;
import com.example.spider.provider.why10w.dto.WhyTaskResult;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 处理 why10 详情页结果
 *
 * @author lym
 */
@Slf4j
@Component
public class Why10DetailResultHandler extends AbstractWhy10wDetailResultHandler {

    /**
     * handle
     */
    @Override
    public void doHandle(Why10wDetailTask task, String html) {
        // 要解析的太多了，先截取期望部分，避免每次解析全部html entry-content entry-tags <a>
        String hasContentRegex = "gridlove-sidebar-right(.*?)<div class=\"copyright\">";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, html);
        if (contentStr.isEmpty()) {
            // 结束了
            log.error("why10Detail invalid. Task:{}", task);
            return;
        }

        String content = contentStr.get(0);

        String tagRegex = "<a href=\".*?&#039; Tag\">(.*?)</a>";
        List<String> tagList = ReUtil.findAllGroup1(tagRegex, content);

        String contentRegex = "<div class=\"entry-content\">(.*?)<div class=\"entry-tags\">";
        if (CollectionUtils.isEmpty(tagList)) {
            contentRegex = "<div class=\"entry-content\">(.*)";
        }
        List<String> contentFindResult = ReUtil.findAllGroup1(contentRegex, content);
        String realContent = "";
        if (CollectionUtils.isEmpty(contentFindResult)) {
            // 不存在
            log.warn("empty");
        } else {
            realContent = Jsoup.parse(contentFindResult.get(0)).text();
        }

        Why10CategoryEnum categoryEnum = Why10CategoryEnum.valueOf(task.getFirstCategory());
        WhyTaskResult result = WhyTaskResult.builder()
                .id(task.getId())
                .title(task.getTitle())
                .categoryList(List.of(categoryEnum.getFirstCategory(), categoryEnum.getSecondCategory()))
                .url(task.getUrl())
                .source(task.getSource())
                .content(realContent)
                .tagList(tagList)
                .build();

        String imageUrlRex = "src=\"(.*?)\" class=\"attachment-gridlove-single size-gridlove-single wp-post-image";
        List<String> imageUrlList = ReUtil.findAllGroup1(imageUrlRex, content);
        if (!CollectionUtils.isEmpty(imageUrlList)) {
            List<WhyImageInfo> imageUrl = imageUrlList.stream().map(imgUrl -> {
                // 页面未标记图片名称，这里没有 name
                return WhyImageInfo.builder().url(imgUrl).build();
            }).collect(Collectors.toList());
            result.setImageInfoList(imageUrl);
        }

        // 保存
        repository.addResult(result);

        repository.saveHtml(task, html);
    }
}
